﻿#include "stdafx.h"
#include "package.h"
#include <concrt.h>


using namespace Concurrency;

static const int BufferSize = 512*1024;

void cvIplToJpeg(IplImage * pImg, BYTE jpeg[], int & jpegSize, const char* filename)
{
    static critical_section cs;
    cs.lock();

    char fileName[] = "temp.jpg";

    jpegSize = 0;

    if (jpeg == NULL)
    {
        return;
    }

    cvSaveImage(fileName, pImg);

    FILE *fp = fopen(fileName, "rb");
        fseek(fp, 0, SEEK_END);
        jpegSize = ftell(fp);
        assert(jpegSize > 0);
        fseek(fp, 0, SEEK_SET);
        int count = fread(jpeg, jpegSize, 1, fp);
        assert(count == 1);
        fclose(fp);

    remove(filename);
    cs.unlock();
}


//实时图像
int PackImage(CJSByteArray &byteArray, ImageInfo &image, CDIB &fdib)
{

    BYTE jpegBuffer[BufferSize];
    ZeroMemory(jpegBuffer, BufferSize);

    int len = 0;
    IplImage *pImg = cvCvtDIB2IMG(&fdib);
    cvIplToJpeg(pImg, jpegBuffer, len, "a.jpeg");
    cvReleaseImage(&pImg);
    pImg = NULL;
    
    
    image.len = len;

    byteArray.PutInt(htonl(MSG_IMAGE));

    byteArray.PutLong(htonl(image.time));
    byteArray.PutByte(image.ATMno);
    byteArray.PutByte(image.cardno);
    byteArray.PutShort(htons(image.addr));
    byteArray.PutByte(image.type);
    byteArray.PutInt(htonl(image.len));
    byteArray.PutByte(jpegBuffer, image.len);


//	printf("image packets, packlen=%d, type=%x, cardno=%x, imagelen=%d\n",\
//			byteArray.Length(), *(BYTE*)byteArray.GetData(), *(BYTE*)(byteArray.GetData()+9), image.len);


    return byteArray.Length();
}
int PackImage(CJSByteArray &byteArray, ImageInfo &image, IplImage &lplImage)
{
    
    BYTE jpegBuffer[BufferSize];
    ZeroMemory(jpegBuffer, BufferSize);

    int len = 0;
    cvIplToJpeg(&lplImage, jpegBuffer, len, "a.jpeg");
        
    image.len = len;

    byteArray.PutInt(htonl(MSG_IMAGE));

    byteArray.PutLong(htonl(image.time));
    byteArray.PutByte(image.ATMno);
    byteArray.PutByte(image.cardno);
    byteArray.PutShort(htons(image.addr));
    byteArray.PutByte(image.type);
    byteArray.PutInt(htonl(image.len));
    byteArray.PutByte(jpegBuffer, image.len);


    return byteArray.Length();
}
//报警信息
int PackAlarm(CJSByteArray &byteArray, ATM_AlarmInfo &alarm, Rects &rects, CDIB &fdib)
{

    static BYTE jpegBuffer[BufferSize];
    ZeroMemory(jpegBuffer, BufferSize);
    int len = 0;
    IplImage *pImg = cvCvtDIB2IMG(&fdib);
    cvIplToJpeg(pImg, jpegBuffer, len, "a.jpeg");
    cvReleaseImage(&pImg);
    pImg = NULL;

    alarm.exImageLen = len;

    byteArray.PutInt(htonl(MSG_ALARM));

    byteArray.PutLong(htonl(alarm.time));
    byteArray.PutInt(htonl(alarm.warnID));
    byteArray.PutByte(alarm.ATMno);
    byteArray.PutByte(alarm.cardno);
    byteArray.PutShort(htons(alarm.addr));
    byteArray.PutByte(alarm.type);
    byteArray.PutByte(alarm.level);
    byteArray.PutInt(htonl(alarm.exImageLen));
    printf("PackAlarm len = %d\n", alarm.exImageLen);
    byteArray.PutShort(htons(alarm.rectNum));

    byteArray.PutByte(jpegBuffer, alarm.exImageLen);

    Rects::iterator beginIt = rects.begin();
    Rects::iterator endIt = rects.end();
    while (beginIt != endIt)
    {
        byteArray.PutShort(htonl(beginIt->x));
        byteArray.PutShort(htonl(beginIt->y));
        byteArray.PutShort(htonl(beginIt->width));
        byteArray.PutShort(htonl(beginIt->height));

        beginIt++;
    }
    return byteArray.Length();
}

int PackAlarm(CJSByteArray &byteArray, ATM_AlarmInfo &alarm, Rects &rects, IplImage &lplImage)
{

    BYTE jpegBuffer[BufferSize];
    ZeroMemory(jpegBuffer, BufferSize);
    
    int len = 0;
    cvIplToJpeg(&lplImage, jpegBuffer, len, "a.jpeg");

   
    alarm.exImageLen = len;

    byteArray.PutInt(htonl(MSG_ALARM));

    byteArray.PutLong(htonl(alarm.time));
    byteArray.PutInt(htonl(alarm.warnID));
    byteArray.PutByte(alarm.ATMno);
    byteArray.PutByte(alarm.cardno);
    byteArray.PutShort(htons(alarm.addr));
    byteArray.PutByte(alarm.type);
    byteArray.PutByte(alarm.level);
    byteArray.PutInt(htonl(alarm.exImageLen));
    byteArray.PutShort(htons(alarm.rectNum));

    byteArray.PutByte(jpegBuffer, alarm.exImageLen);

    Rects::iterator beginIt = rects.begin();
    Rects::iterator endIt = rects.end();
    while (beginIt != endIt)
    {
        byteArray.PutShort(htonl(beginIt->x));
        byteArray.PutShort(htonl(beginIt->y));
        byteArray.PutShort(htonl(beginIt->width));
        byteArray.PutShort(htonl(beginIt->height));

        beginIt++;
    }
    return byteArray.Length();
}
//交易信息
int PackTradeInfo(CJSByteArray &byteArray, ATM_FaceInfo &trade, CDIB &originfdib,
        CDIB &facefdib)
{

    BYTE originjpegBuffer[BufferSize];
    ZeroMemory(originjpegBuffer, BufferSize);
    BYTE facejpegBuffer[BufferSize];
    ZeroMemory(facejpegBuffer, BufferSize);

    int oLen = 0;
    IplImage *pOImg = cvCvtDIB2IMG(&originfdib);
    cvIplToJpeg(pOImg, originjpegBuffer, oLen, "a.jpeg");
    cvReleaseImage(&pOImg);
    pOImg = NULL;

    int fLen = 0;
    IplImage *pFImg = cvCvtDIB2IMG(&facefdib);
    cvIplToJpeg(pFImg, facejpegBuffer, fLen, "f.jpeg");
    cvReleaseImage(&pFImg);
    pFImg = NULL;

   
    trade.originImageLen = oLen;

   
    trade.faceImageLen = fLen;

    byteArray.PutInt(htonl(MSG_TRADEINFO));


    byteArray.PutString(trade.ICcardno, 32);
    byteArray.PutByte(trade.ATMno);
    byteArray.PutByte(trade.cardno);
    byteArray.PutShort(htons(trade.addr));
    byteArray.PutLong(htonl(trade.time));
    byteArray.PutInt(htonl(trade.originImageLen));
    byteArray.PutInt(htonl(trade.faceImageLen));

    byteArray.PutByte(originjpegBuffer, trade.originImageLen);
    byteArray.PutByte(facejpegBuffer, trade.faceImageLen);
    return byteArray.Length();
}

int PackTradeInfo(CJSByteArray &byteArray, ATM_FaceInfo &trade,
        IplImage &originlplImage, IplImage &facelplImage)
{
 
    BYTE originjpegBuffer[BufferSize];
    ZeroMemory(originjpegBuffer, BufferSize);
    BYTE facejpegBuffer[BufferSize];
    ZeroMemory(facejpegBuffer, BufferSize);

    int oLen = 0;
    cvIplToJpeg(&originlplImage, originjpegBuffer, oLen, "o.jpeg");

    int fLen = 0;
    cvIplToJpeg(&facelplImage, facejpegBuffer, fLen, "f.jpeg");

   
    trade.originImageLen = oLen;
   
    trade.faceImageLen = fLen;

    byteArray.PutInt(htonl(MSG_TRADEINFO));

    byteArray.PutString(trade.ICcardno, 32);
    byteArray.PutByte(trade.ATMno);
    byteArray.PutByte(trade.cardno);
    byteArray.PutShort(htons(trade.addr));
    byteArray.PutLong(htonl(trade.time));
    byteArray.PutInt(htonl(trade.originImageLen));
    byteArray.PutInt(htonl(trade.faceImageLen));

    byteArray.PutByte(originjpegBuffer, trade.originImageLen);
    byteArray.PutByte(facejpegBuffer, trade.faceImageLen);
    return byteArray.Length();
}

int PackSystemStatus(CJSByteArray &byteArray, SystemStatus &system)
{

    byteArray.PutInt(htonl(MSG_SYSTEMSTATUS));

    byteArray.PutInt(htonl(system.time));
    byteArray.PutByte(system.cpu);
    byteArray.PutByte(system.mem);
    byteArray.PutByte(system.ATMno);
    byteArray.PutByte(system.hVideoStatus);
    byteArray.PutByte(system.vVideoStatus);
    return byteArray.Length();
}

int PackCameraBlockPack(CJSByteArray &byteArray, CameraBlockPack &cameraBlock)
{
    byteArray.PutInt(htonl(MSG_CAMERABLOCK));
    byteArray.PutLong(htonl(cameraBlock.time));

    CDIB *pDib = cvCvtIMG2DIB(cameraBlock.pImg);
    static BYTE jpegBuffer[512 * 1024];
    int len = 0;
    
    cvIplToJpeg(cameraBlock.pImg, jpegBuffer, len, "temp.jpg");

   
    delete pDib;
    pDib = NULL;

    byteArray.PutInt(htonl(len));
    printf("len------------------%d\n", len);
    byteArray.PutByte(jpegBuffer, len);

    return len;

}

BYTE check(BYTE *buf, int length)
{
    BYTE checkSum = 0;
    for (int i = 0; i < length; ++i)
    {
        checkSum += buf[i];
    }

    return 0xff - checkSum;
}

